capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local manasi "";
local output "";
local log "";
local data "";
local earn "";

log using "`log'\restat_analysis_rd_polyestearn.log", replace;

/* 
Manasi Deshpande, 11/07/2012
RD estimates and graphs (earnings)
*/

use "`earn'\analysis04_entryrd_earn.dta", clear;

local exp "entry04rd";
local time "wk";
local unit "weekly";
local tlabel_dte "01oct2000 01jan2001 01apr2001 01jul2001 01oct2001 01jan2002 01apr2002 01jul2002 01oct2002, format(%tdmd)";
local tlabel_wk "2001w1 2001w14 2001w27 2001w40 2002w1 2002w14 2002w27";
local tline_dte "01oct2001";
local tline_wk "2001w40";


/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

/* Fiscal YOB */
destring cdr_dobmm, replace;
destring cdr_dobdd, replace;
gen cdr_dob_stata=mdy(cdr_dobmm, cdr_dobdd, cdr_dobyy);
gen n_fiscalyob=.;
forval yr=1991(1)2002 {;
	replace n_fiscalyob=`yr' if cdr_dob_stata>=mdy(10,01,`=`yr'-1') & cdr_dob_stata<mdy(10,01,`yr');
};
tab n_fiscalyob, m;


/* ***************************************** */
/* **** PREP EARNINGS OUTCOME VARIABLES **** */
/* ***************************************** */

rename n_combpay_2yr n_combpay2yr;
rename n_combpay_5yr n_combpay5yr;
rename n_combpay_9yr n_combpay9yr;
order n_hhearns0206_* n_combpay_19* n_combpay_20*;

/* Create total parental earnings before age 18 */
forval yr=1991(1)1993 {;
	egen n_hhearn020618_`yr'=rowtotal(n_hhearns0206_`yr'-n_hhearns0206_`=`yr'+18') if cdr_dobyy==`yr';
};

forval yr=1994(1)2002 {;
	egen n_hhearn020618_`yr'=rowtotal(n_hhearns0206_`yr'-n_hhearns0206_2011) if cdr_dobyy==`yr';
};

gen n_hhearn020618=.;
forval yr=1991(1)2002 {;
	replace n_hhearn020618=n_hhearn020618_`yr' if cdr_dobyy==`yr';
};
summ n_hhearn020618, det;
drop n_hhearn020618_*;

/* Create total parental earnings after CDR event in FY2004 */
egen n_hhearn0206post04=rowtotal(n_hhearns0206_2004-n_hhearns0206_2011);
replace n_hhearn0206post04=n_hhearn0206post04/8;
egen n_hhearn0206post03=rowtotal(n_hhearns0206_2003-n_hhearns0206_2011);
replace n_hhearn0206post03=n_hhearn0206post03/9;

gen byte n_hhearngt0=(n_hhearn0206post03>0);
gen byte n_hhearngt10=(n_hhearn0206post03>10000);
gen byte n_hhearngt20=(n_hhearn0206post03>20000);
gen byte n_hhearngt30=(n_hhearn0206post03>30000);

/* Create total parental earnings before CDR event */
egen n_hhearn0206pre04=rowtotal(n_hhearns0206_1985-n_hhearns0206_2003);
replace n_hhearn0206pre04=n_hhearn0206pre04/19;
egen n_hhearn0206pre03=rowtotal(n_hhearns0206_1985-n_hhearns0206_2002);
replace n_hhearn0206pre03=n_hhearn0206pre03/18;

/* Create total household income variable (earned+unearned)*/
forval  yr=1985(1)2011 {;
	summ n_hhunearninc0206_`yr', det;
	gen n_tothhinc0206_`yr'=n_hhearns0206_`yr'+n_hhunearninc0206_`yr' if n_hhearns0206_`yr'!=.;
	summ n_tothhinc0206_`yr', det;
};

/* Create total household income post-CDR */
egen n_tothhinc0206post04=rowtotal(n_tothhinc0206_2004-n_tothhinc0206_2011);
replace n_tothhinc0206post04=n_tothhinc0206post04/8;
egen n_tothhinc0206post03=rowtotal(n_tothhinc0206_2003-n_tothhinc0206_2011);
replace n_tothhinc0206post03=n_tothhinc0206post03/9;

/* Create total household income pre-CDR */
egen n_tothhinc0206pre04=rowtotal(n_tothhinc0206_1985-n_tothhinc0206_2003);
replace n_tothhinc0206pre04=n_tothhinc0206pre04/19;
egen n_tothhinc0206pre03=rowtotal(n_tothhinc0206_1985-n_tothhinc0206_2002);
replace n_tothhinc0206pre03=n_tothhinc0206pre03/18;

order
	n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
	n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
	n_hhearns0206_*
	n_tothhinc0206_*
	;
	
/* Create squared pre-treatment earnings */
egen n_hhearn0206cov=rowmean(n_hhearns0206_1998-n_hhearns0206_2002);
gen n_hhearn0206cov_2=n_hhearn0206cov*n_hhearn0206cov;
forval yr=1985(1)2002 {;
	gen n_hhearns0206_`yr'_2=n_hhearns0206_`yr'*n_hhearns0206_`yr';
};

/* Coefficient of variation for earnings and income */
foreach stub in hhearns tothhinc {;
	egen tempsd_`stub'=rowsd(n_`stub'0206_2003-n_`stub'0206_2011);
	egen tempmean_`stub'=rowmean(n_`stub'0206_2003-n_`stub'0206_2011);
	gen cv_`stub'=tempsd_`stub'/tempmean_`stub';
	replace cv_`stub'=0 if tempmean_`stub'==0 & cv_`stub'==.;
	drop temp*;
};


/* *********************************** */
/* **** PREP TO MAKE SCATTER PLOT **** */
/* *********************************** */

/* Create a Stata date variable for award date */
destring awddte_mm, replace;
destring awddte_dd, replace;
destring awddte_yr, replace;
gen awddte_stata=mdy(awddte_mm, awddte_dd, awddte_yr);
gen awddte_run2=awddte_stata-mdy(10,01,2001);
assert awddte_run==awddte_run2;
drop awddte_run2;
gen awdwk_stata=wofd(awddte_stata);
gen awdwk_run=awdwk_stata-wofd(mdy(10,01,2001));

egen bin=group(awdwk_stata);
forval i=1(1)82 {;
	gen byte bin_`i'=(bin==`i');
};

/* Sort on award date */
sort awddte;
order fy hun cdr_dobyy cdr_mddate awddte n_firstpayyr fy_2004 n_fy01ent n_fy02ent;

/* Take out FYs affected by Deputy Commissioner's hold on ages 13-17 */
tab n_fiscalyob, m;
drop if n_fiscalyob==1991 | n_fiscalyob==1992 | n_fiscalyob==2002;
tab n_fiscalyob, m;

gen byte n_fam_singmom=(par2_sex=="" & par1_sex=="F");
gen byte n_fam_twopar=(par1_sex!="" & par2_sex!="");
gen byte n_fam_oldpar=(par1_dobyy>=1973 & par1_dobyy!=.);
gen byte n_fam_youngpar=(par1_dobyy<1973 & par1_dobyy!=.);


/* ************************ */
/* **** RD CALCULATION **** */
/* ************************ */
gen awddte_run2=awddte_run*awddte_run;
gen awddte_run3=awddte_run2*awddte_run;
gen awddte_run4=awddte_run3*awddte_run;

gen fy01Xawddte=n_fy01ent*awddte_run;
gen fy01Xawddte2=n_fy01ent*awddte_run2;
gen fy01Xawddte3=n_fy01ent*awddte_run3;
gen fy01Xawddte4=n_fy01ent*awddte_run4;

local poly_1 "awddte_run fy01Xawddte";
local poly_2 "awddte_run awddte_run2 fy01Xawddte fy01Xawddte2";
local poly_3 "awddte_run awddte_run2 awddte_run3 fy01Xawddte fy01Xawddte2 fy01Xawddte3";
local poly_4 "awddte_run awddte_run2 awddte_run3 awddte_run4 fy01Xawddte fy01Xawddte2 fy01Xawddte3 fy01Xawddte4";


foreach bwidth in 150 200 250 {; 

forval poly=1(1)4 {;

	reg n_hhearn020618 n_fy01ent `poly_`poly'' 
		if abs(awddte_run)<=`bwidth'
		, robust;
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'nocov.xls", e(N r2 rmse) replace;
	xi: reg n_hhearn020618 n_fy01ent `poly_`poly''
		n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
		if abs(awddte_run)<=`bwidth'
		, robust;
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'allcov.xls", e(N r2 rmse) replace;
	xi: reg n_hhearn020618 n_fy01ent `poly_`poly''
		n_male i.n_fiscalyob i.cdr_state i.n_diag1cat 
		n_hhearns0206_1985-n_hhearns0206_2002 n_hhearns0206_1985_2-n_hhearns0206_2002_2
		if abs(awddte_run)<=`bwidth'
		, robust;
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'prehist2allcov.xls", e(N r2 rmse) replace;
		
	foreach var of varlist 
			n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
			n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
			n_hhearngt*
			cv_hhearns cv_tothhinc 
			n_hhearns0206_1985-n_hhearns0206_2011
			n_tothhinc0206_1985-n_tothhinc0206_2011
		{;
		reg `var' n_fy01ent `poly_`poly'' 
			if abs(awddte_run)<=`bwidth'
			, robust;
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'nocov.xls", e(N r2 rmse) append;
		xi: reg `var' n_fy01ent `poly_`poly''
			n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
			if abs(awddte_run)<=`bwidth'
			, robust; 
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'allcov.xls", e(N r2 rmse) append;
		};

	foreach var of varlist 
			n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
			n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
			n_hhearngt*
			cv_hhearns cv_tothhinc 
			n_hhearns0206_2003-n_hhearns0206_2011
			n_tothhinc0206_2003-n_tothhinc0206_2011		
		{;	
		xi: reg `var' n_fy01ent `poly_`poly''
			n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
			n_hhearns0206_1985-n_hhearns0206_2002 n_hhearns0206_1985_2-n_hhearns0206_2002_2
			if abs(awddte_run)<=`bwidth'
			, robust; 
		outreg2 using "`output'\\`exp'_man`bwidth'_earn_`poly'prehist2allcov.xls", e(N r2 rmse) append;
		};
};

/* Test joint significance of bin dummies  */
forval poly=1(1)4 {;

	xi: reg n_hhearn020618 n_fy01ent `poly_`poly'' `bin`bwidth''
		if abs(awddte_run)<=`bwidth'
		, robust;
		test `bin`bwidth'';
		outreg2 using "`output'\\`exp'_man`bwidth'_bins`poly'earn.xls", 
			adds(F-test, r(F), Prob > F, `r(p)') e(rmse) replace;

	foreach var of varlist 
			n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
			n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
			n_hhearngt*
			cv_hhearns cv_tothhinc n_delta_hhearnspost n_delta_tothhincpost
			n_yrhhinclt10 n_yrhhinclt20 n_yrhhinclt30
			gamma0_ce gamma10_ce gamma15_ce gamma20_ce gamma25_ce gamma30_ce gamma35_ce gamma40_ce
			n_hhearns0206_2003-n_hhearns0206_2011
			n_tothhinc0206_2003-n_tothhinc0206_2011	
		{;
		xi: reg `var' n_fy01ent `poly_`poly'' `bin`bwidth''
		if abs(awddte_run)<=`bwidth'
			, robust;
			test `bin`bwidth''; 
			outreg2 using "`output'\\`exp'_man`bwidth'_bins`poly'earn.xls", 
				adds(F-test, r(F), Prob > F, `r(p)') e(rmse) append;
		};
};

/* Statsby */
forval poly=1(1)4 {;

	foreach var of varlist 
		n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
		n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
		n_hhearngt*
		cv_hhearns cv_tothhinc 
		n_hhearns0206_1985-n_hhearns0206_2011
		n_tothhinc0206_1985-n_tothhinc0206_2011
	{;

	statsby _b _se, 
		saving("`data'\statsby\\`exp'_man`bwidth'_`var'_`poly'nocov.dta", replace):
			reg `var' n_fy01ent `poly_`poly'' 
				if abs(awddte_run)<=`bwidth'
				, robust;
				
	xi: statsby _b _se, 
		saving("`data'\statsby\\`exp'_man`bwidth'_`var'_`poly'allcov.dta", replace):
			reg `var' n_fy01ent `poly_`poly''
				n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
				if abs(awddte_run)<=`bwidth'
				, robust; 
	};

foreach var of varlist 
		n_hhearns0206_2003-n_hhearns0206_2011
		n_tothhinc0206_2003-n_tothhinc0206_2011
					
	{;	
		xi: statsby _b _se, 
		saving("`data'\statsby\\`exp'_man`bwidth'_`var'_`poly'prehistallcov.dta", replace):
			reg `var' n_fy01ent `poly_`poly'' 
				n_hhearns0206_1985-n_hhearns0206_2002
				n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
				if abs(awddte_run)<=`bwidth'
				, robust; 

	xi: statsby _b _se, 
		saving("`data'\statsby\\`exp'_man`bwidth'_`var'_`poly'prehist2allcov.dta", replace):
			reg `var' n_fy01ent `poly_`poly'' 
				n_hhearns0206_1985-n_hhearns0206_2002 n_hhearns0206_1985_2-n_hhearns0206_2002_2
				n_male i.n_fiscalyob i.cdr_state i.n_diag1cat
				if abs(awddte_run)<=`bwidth'
				, robust; 
	};
};
};

/* GET MEANS */
summ
	n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
	n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
	n_hhearngt*
	cv_hhearns cv_tothhinc 
	n_hhearns0206_1985-n_hhearns0206_2011
	n_tothhinc0206_1985-n_tothhinc0206_2011
;

summ
	n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
	n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
	n_hhearngt*
	cv_hhearns cv_tothhinc 
	n_hhearns0206_1985-n_hhearns0206_2011
	n_tothhinc0206_1985-n_tothhinc0206_2011
if n_fy01ent;

summ
	n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
	n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
	n_hhearngt*
	cv_hhearns cv_tothhinc 
	n_hhearns0206_1985-n_hhearns0206_2011
	n_tothhinc0206_1985-n_tothhinc0206_2011
if !n_fy01ent;


/* COLLAPSE ON DAY OR WEEK */
preserve;
collapse 	(mean) awd`time'_stata 
	n_hhearn020618 n_hhearn0206post04 n_hhearn0206post03 n_tothhinc0206post04 n_tothhinc0206post03
	n_hhearn0206pre04 n_hhearn0206pre03 n_tothhinc0206pre04 n_tothhinc0206pre03
	n_hhearngt*
	cv_hhearns cv_tothhinc 
	n_hhearns0206_1985-n_hhearns0206_2011
	n_tothhinc0206_1985-n_tothhinc0206_2011
	(count) awddte, 
	by(awd`time'_run);
rename awddte count;
list count;
save "`data'\statsby\restat_analysis_rd_earn_graphs.dta", replace;

log close;
